import { AddCommdity, AddTeaItemParam, AddTypeParam, CommodityListType, EditCommdity, EnableCommdityParams, GetTeaListParam, SearchListQuery, TeaItemListType } from "@Tea/model"
import { CommondityRepository } from "../repo/CommodityRepository"
import { TokenObj } from "../dao/Token"
import { TeaItemsRepositoty } from "../repo/TeaItemsRepositoty"

export class CommondityManageCtx {
  private static inst: CommondityManageCtx
  private commondityRepo: CommondityRepository = CommondityRepository.getInstance()
  private teaItemRepo: TeaItemsRepositoty = TeaItemsRepositoty.getInstance()

  public static getInstance() {
    if (!CommondityManageCtx.inst) {
      CommondityManageCtx.inst = new CommondityManageCtx()
    }

    return CommondityManageCtx.inst
  }

  getPageList(params: GetTeaListParam) {
    return this.commondityRepo.getPageList(params)
  }

  addCommdity(params: AddCommdity, hander: TokenObj['userId']) {
    return this.commondityRepo.addCommdity(params, hander)
  }

  editCommdity(params: EditCommdity, hander: TokenObj['userId']) {
    return this.commondityRepo.editCommdity(params, hander)
  }

  enableCommdity(params: EnableCommdityParams, hander: TokenObj['userId']) {
    return this.commondityRepo.enableCommdity(params, hander)
  }

  // 获取茶叶详情图
  async getTeaItems(teaId: CommodityListType['teaId']) {
    return (await this.teaItemRepo.getTeaItems(teaId))[0]
  }
  addTeaItem(params: AddTeaItemParam) {
    return this.teaItemRepo.addTeaItem(params)
  }
  delTeaItem(itemId: TeaItemListType['itemId']) {
    return this.teaItemRepo.delTeaItem(itemId)
  }

  // 查询列表
  async searchList(query: SearchListQuery) {
    return (await this.commondityRepo.searchList(query))[0]
  }
}